package com.handu.luna.boss.controller;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.handu.luna.core.domain.Context;
import com.handu.luna.core.domain.Session;
import com.handu.luna.core.domain.admin.AdminControllerDescription;
import com.handu.luna.core.domain.admin.Operator;
//import com.handu.luna.core.domain.support.StringEscapeEditor;

@Transactional
public abstract class BaseController {
	public static final int DEFAULT_PAGE_SIZE = 10;
	
	private Log logger = LogFactory.getLog(this.getClass());
		
	@Autowired
	Context context;
	
	@Autowired
	EntityManager entityManager;
	
	void DEBUG(Object message){
		if(logger.isDebugEnabled()) logger.debug(message);
	}
	void DEBUG(Object message,Throwable t){
		if(logger.isDebugEnabled()) logger.debug(message, t);
	}	
	
	void INFO(Object message){
		logger.info(message);
	}
	void ERROR(Object message,Throwable t){
		logger.error(message);
	}	
	
	public void init(){
		
	}	
	
    @InitBinder()
    public void initBinder(WebDataBinder binder){
    	//binder.registerCustomEditor(String.class, new StringEscapeEditor(true, true, true));
    }	
	
    private AdminControllerDescription description;
    
	public AdminControllerDescription getDescription() {
		return description;
	}
	public void setDescription(AdminControllerDescription description) {
		this.description = description;
	}
	
	private Operator operator;

	public Operator getOperator() {
		return operator;
	}
	public void setOperator(Operator operator) {
		this.operator = operator;
	}
	
	private Session session;

	public Session getSession() {
		return session;
	}
	public void setSession(Session session) {
		this.session = session;
	}
	BossModel createModel(){
		return createModel(null);	
	}
	BossModel createModel(String viewName){
		BossModel model = new BossModel();
		model.setEntityManager(entityManager);
		model.setViewName(viewName);
		if(viewName!= null && (viewName.contains("redirect:") ||viewName.contains("forward:") )){
			//do nothing
		}else{
			model.add(LoginController.OPERATOR_FLAG, getOperator());
		}
		return model;	
	}	

}
